package cn.itsource.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.Log;
import cn.itsource.exception.BusinessException;
import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.dto.LoginDto;
import cn.itsource.system.jwt.JwtUtil;
import cn.itsource.system.jwt.Payload;
import cn.itsource.system.mapper.LogininfoMapper;
import cn.itsource.system.query.LogininfoQuery;
import cn.itsource.system.service.ILogininfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 业务实现类：登录信息Logininfo
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class LogininfoServiceImpl implements ILogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Logininfo> findAll() {
        return logininfoMapper.findAll();
    }

    @Override
    public Logininfo findOne(Long id) {
        return logininfoMapper.findOne(id);
    }

    @Transactional
    @Override
    @Log
    public void add(Logininfo logininfo) {
        logininfoMapper.add(logininfo);
    }

    @Transactional
    @Override
    @Log
    public void update(Logininfo logininfo) {
        logininfoMapper.update(logininfo);
    }

    @Transactional
    @Override
    @Log
    public void delete(Long id) {
        logininfoMapper.delete(id);
    }

    @Transactional
    @Override
    @Log
    public void patchDel(Long[] ids) {
        logininfoMapper.patchDel(ids);
    }

    @Override
    public PageInfo<Logininfo> queryPage(LogininfoQuery logininfoQuery) {
        //开启分页
        PageHelper.startPage(logininfoQuery.getCurrentPage(),logininfoQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<Logininfo> list = logininfoMapper.queryPage(logininfoQuery);
        //调用PageInfo的有参会计算其他参数
        PageInfo<Logininfo> pageInfo = new PageInfo<Logininfo>(list);
        return pageInfo;
    }

    @Override
    public String login(LoginDto loginDto) {
        String account = loginDto.getAccount();
        String password = loginDto.getPassword();
        //1.校验-空值
        if(StrUtil.isBlank(account) || StrUtil.isBlank(password)){
            throw new BusinessException("信息不能为空");
        }
        //2.校验-账号
        Logininfo logininfo = logininfoMapper.findByAccount(loginDto);
        if(logininfo == null){
            throw new BusinessException("账号不存在");
        }
        //3.校验-密码【加密加盐-hutool工具包】
        String md5Pwd = SecureUtil.md5(password + logininfo.getSalt());
        if(!md5Pwd.equals(logininfo.getPassword())){
            throw new BusinessException("密码错误");
        }
        //4.登录成功：1.登录信息有了 2.当前登录人的权限 3.前登录人的菜单 ->Payload
        Payload payload = new Payload();
        logininfo.setPassword(null);
        payload.setLogininfo(logininfo);
        //获取当前登录人的权限 - 后面做按钮权限 - 按钮权限只需要permission的sn属性值即可
        List<String> permissions = logininfoMapper.findPermissionsByLogininfoId(logininfo.getId());
        //获取当前登录人的菜单 - 从路由中可以知道，菜单需要很多数据
        List<Menu> menus = logininfoMapper.findMenusByLogininfoId(logininfo.getId());
        payload.setPermissions(permissions);
        payload.setMenus(menus);

        //5.调用JwtUtils生成jwtToken - 响应给Controller
        return JwtUtil.generateToken(payload);
    }

}
